package com.getword.web.controller;


import com.getword.domain.Reply;
import com.getword.domain.User;
import com.getword.domain.VideoComment;
import com.getword.service.VideoCommentService;
import com.getword.utils.SpringContextUtil;
import com.getword.web.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/api/videoComment")
public class ApiVideoCommentController {
    @Autowired
    private VideoCommentService service;

    @GetMapping("/{videoId}")
    public Result query(@PathVariable String videoId){
        return service.getCommentsByVideoId(videoId);
    }

    // 添加视频评论,user
    @PostMapping
    Result create(VideoComment comment){
        // user
        User user = SpringContextUtil.getLoginUser();
        comment.setUser(user);
        comment.setTime(new Date());
        return service.saveComment(comment);
    }

    @PostMapping
    @RequestMapping("/addReply")
    public Result addReply(Reply reply, String commentId, HttpServletRequest request){
        reply.setId(UUID.randomUUID().toString());
        reply.setTime(new Date());
        User user = SpringContextUtil.getLoginUser();
        Map<String, Object> from = new HashMap<>();
        from.put("id", user.getId());
        from.put("username", user.getUsername());
        reply.setFrom(from);
        Map<String, Object> to = new HashMap<>();
        to.put("id", request.getParameter("toId"));
        to.put("username", request.getParameter("toUsername"));
        reply.setTo(to);
        return service.addReply(reply, commentId);
    }
}
